import {
	asyncRoutes,
	constantRoutes,
	components
} from '@/router'
import {
	getBackstage
} from '@/api/role'
import Layout from '@/layout'




/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
	if (route.meta && route.meta.roles) {
		return roles.some(role => route.meta.roles.includes(role))
	} else {
		return true
	}
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
	const res = []

	routes.forEach(route => {
		const tmp = {
			...route
		}
		if (hasPermission(roles, tmp)) {
			if (tmp.children) {
				tmp.children = filterAsyncRoutes(tmp.children, roles)
			}
			res.push(tmp)
		}
	})

	return res
}
// 后台路由表处理
export function disposeRoutes(routes) {
	const res = []

	routes.forEach(route => {
		let tmp = JSON.parse(route.url)
		if (tmp.type === 1) {
			// 首页配置
			if (!tmp.path || tmp.path == '/') {
				tmp.redirect = '/dashboard',
					tmp.children[0].name = 'Dashboard'
				tmp.children[0].path = 'dashboard'
			}
			tmp['component'] = Layout
			if (tmp.path !== '/') {
				tmp['path'] = (tmp.path.substr(0, 1) == '/' ? '':'/') + tmp.path
			}
			tmp.children[0].component = components[tmp.children[0].component]
			tmp.children[0].meta.noCache = tmp.children[0].meta.noCache ? false : true
			tmp['meta'] = tmp.children[0].meta

			res.push(tmp)
		} else if (tmp.type === 2) {
			tmp['component'] = Layout
			tmp['path'] = (tmp.path.substr(0, 1) == '/' ? '':'/') + tmp.path
			let childrenList = route.children
			childrenList.forEach(route => {
				let item = JSON.parse(route.url)
				let res = {}
				res['component'] = components[item.children[0].component]
				res['path'] = (item.path.substr(0, 1) == '/' ? '':'/') + item.path
				res['name'] = item.children[0].name
				item.children[0].meta.noCache = item.children[0].meta.noCache ? false : true
				res['meta'] = item.children[0].meta
				tmp.children.push(res)
			})
			res.push(tmp)
		}
	})
	return res
}


const state = {
	routes: [],
	addRoutes: [],
	navs:[],
}

const mutations = {
	SET_ROUTES: (state, routes) => {
		state.addRoutes = routes
		state.routes = constantRoutes.concat(routes)
	},
	SET_NAVS: (state, navs) => {
		state.navs = navs
	},
}

const actions = {
	generateRoutes({commit}, roles) {
		return new Promise(resolve => {
			let accessedRoutes
			if (roles.includes('admin')) {
				accessedRoutes = asyncRoutes || []
			} else {
				accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
			}
			commit('SET_ROUTES', accessedRoutes)
			
			resolve(accessedRoutes)
		})
	},

	getBackstage({commit}, roles) {
		return new Promise(resolve => {
			let accessedRoutes
			getBackstage().then(response => {
				let arr = disposeRoutes(response)
				// console.log('Routes', arr)
				commit('SET_NAVS', arr)
				accessedRoutes = [...asyncRoutes, ...arr]
				commit('SET_ROUTES', accessedRoutes)
				resolve(accessedRoutes)
			}).catch(error => {
				reject(error)
			})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
